package com.sjh.baseui.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import com.sjh.baseui.R
import com.sjh.baseui.databinding.AppExpandGroupBinding
import com.sjh.baseutils.dp
import java.lang.Exception

/**
 * 可展开收起的文本控件
 * @author wangketao
 * @date 2020年11月14日
 */
class ExpandTextView @kotlin.jvm.JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    LinearLayout(context, attrs, defStyleAttr) {
    //文字大小
    var defaultTextSize = 14

    //文字颜色
    var defaultTextColor = 0

    //收缩状态下的max行数
    var defaultShrinkLines = 1

    //收起展开图标
    var arrowIcon = R.drawable.app_arrow_down


    val binding by lazy<AppExpandGroupBinding> {
        DataBindingUtil.inflate(context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater,
                                R.layout.app_expand_group,
                                this,
                                true)
    }

    init {
        if (attrs != null) {
            initAttrs(context, attrs, defStyleAttr)
        }

    }

    fun initAttrs(context: Context, attrs: AttributeSet, defStyleAttr: Int) {
        var array = context.obtainStyledAttributes(attrs, R.styleable.AppExpandView, defStyleAttr, 0)
        defaultTextSize = array.getDimensionPixelSize(R.styleable.AppExpandView_contentTextSize, 14.dp.toInt())
        defaultTextColor = array.getColor(R.styleable.AppExpandView_contentTextColor, Color.parseColor("#FF3F3F3F"))
        defaultShrinkLines = array.getInt(R.styleable.AppExpandView_shrink_lines, 1)
        arrowIcon = array.getResourceId(R.styleable.AppExpandView_arrow_icon, R.drawable.app_arrow_down)
        binding?.let {
            it.tvContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultTextSize.toFloat())
            it.tvContent.setTextColor(defaultTextColor)
            it.tvContentExpand.setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultTextSize.toFloat())
            it.tvContentExpand.setTextColor(defaultTextColor)
            it.tvContent.maxLines = defaultShrinkLines
            it.icArrow.setImageResource(arrowIcon)
        }
    }

    /**
     * 设置文本
     */
    fun setContent(txt: String) {
        binding.icArrow.setTag(false)
        binding.tvContent.text = txt
        binding.tvContentExpand.text = txt
        binding.tvContent.post {
            try {
                var count = binding.tvContent.layout.getEllipsisCount(defaultShrinkLines - 1)
                binding.icArrow.visibility = if (count > 0) View.VISIBLE else View.GONE
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        binding.icArrow.setOnClickListener {
            //默认false 收起
            var isExpand = it.getTag() as Boolean
            binding.tvContent.visibility = if (isExpand) View.VISIBLE else View.GONE
            binding.tvContentExpand.visibility = if (isExpand) View.GONE else View.VISIBLE
            it.animate()?.rotation(if (isExpand) 0f else 180f)?.start()
            it.setTag(!isExpand)
        }
    }
}